package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.service.base.TakeTimeService;
import cn.itcast.crm.domain.Customer;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class FixedAreaAction extends ActionSupport implements ModelDriven<FixedArea> {
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	@Autowired
	private FixedAreaService fixedAreaService;

	private FixedArea fixedArea = new FixedArea();

	@Override
	public FixedArea getModel() {
		// TODO Auto-generated method stub
		return fixedArea;
	}

	@Action(value = "addFixedArea", results = {
			@Result(name = "success", location = "./pages/base/fixed_area.html", type = "redirect") })
	public String addFixedArea() {
		fixedAreaService.addFixedArea(fixedArea);
		return SUCCESS;
	}

	@Action(value = "findAllFixedArea", results = { @Result(name = "success", type = "json") })
	public String findAllFixedArea() {
		Pageable p = new PageRequest(page - 1, rows);
		Specification<FixedArea> sf = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate> list = new ArrayList<>();
				if (StringUtils.isNotBlank(fixedArea.getCompany())) {
					Predicate p = cb.like(root.get("company").as(String.class), fixedArea.getCompany());
					list.add(p);
				}
				if (StringUtils.isNotBlank(fixedArea.getId())) {
					Predicate p = cb.like(root.get("id").as(String.class), fixedArea.getId());
					list.add(p);
				}
				Predicate pp = cb.and(list.toArray(new Predicate[0]));
				return pp;
			}
		};
		Page<FixedArea> pp = fixedAreaService.findAllFixedArea(sf, p);
		Map<String, Object> map = new HashMap<>();
		map.put("total", pp.getTotalElements());
		map.put("rows", pp.getContent());
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}

	@Action(value = "fixedArea_findNoassociationCustomer", results = { @Result(name = "success", type = "json") })
	public String findNoassociationCustomer() {
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9001/crm_management/services/customerService/noAssociationCustomers")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	@Action(value = "fixedArea_findHasAssociationFixedAreaCustomer", results = {
			@Result(name = "success", type = "json") })
	public String findHasAssociationFixedAreaCustomer() {
		Collection<? extends Customer> cc = WebClient
				.create("http://localhost:9001/crm_management/services/customerService/associationCustomers/"
						+ fixedArea.getId())
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(cc);
		return SUCCESS;
	}

	@Action(value = "decidedzone_assigncustomerstodecidedzone", results = {
			@Result(name = "success", location = "./pages/base/fixed_area.html", type = "redirect") })
	public String assigncustomerstodecidedzone() {
	
		String s2 = StringUtils.join(customerIds, ",");
		System.out.println(s2);
		String id = fixedArea.getId();
		WebClient
				.create("http://localhost:9001/crm_management/services/customerService/associationCustomerToFixedArea?customerIds="
						+ s2 + "&FixedAreaId=" + id)
				.accept(MediaType.APPLICATION_JSON).put(null);

		// System.out.println(customerIds.toString());
		return SUCCESS;
	}
	private String courierId;
	
	private String takeTimeId;
	public void setCourierId(String courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(String takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(name="success",location="./pages/base/fixed_area.html",type="redirect")})
	public String associationCourierToFixedArea(){
		System.out.println(courierId + "     " + takeTimeId + "    " + fixedArea.getId());
		fixedAreaService.associationCourierToFixedArea(fixedArea,courierId,takeTimeId);
		return SUCCESS;
	}
}
